package com.chengyu.core.controller.ty;

import com.alibaba.fastjson.JSON;
import com.chengyu.core.component.OperationLog;
import com.chengyu.core.controller.UserBaseController;
import com.chengyu.core.entity.CommonResult;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.model.*;
import com.chengyu.core.response.TyQuoteResponse;
import com.chengyu.core.result.CommonResultCode;
import com.chengyu.core.service.member.MemberService;
import com.chengyu.core.service.ty.TyQuoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "订单")
@Controller
@RequestMapping("/ty/quote")
public class TyQuoteController extends UserBaseController {

    @Autowired
    private TyQuoteService quoteService;


    @Autowired
    private MemberService memberService;


    @ApiOperation(value = "报价")
    @OperationLog
    @ResponseBody
    @RequestMapping(value = {"/add"}, method = RequestMethod.POST)
    public CommonResult<String> quote(String orderNo,
                                      BigDecimal price,
                                      BigDecimal price1,
                                      BigDecimal price2,
                                      BigDecimal price3,
                                      BigDecimal price4,
                                      BigDecimal price5,
                                      String priceDesc) throws ServiceException {
        UmsMember member = getCurrentMember();





        System.out.println("orderNo："+orderNo);

        System.out.println("member.getId()："+member.getId());

        //不能重复报价
       if( quoteService.getQuoteCount(orderNo,member.getId())>0){
           return CommonResult.failed( "不能重复报价");
       }


        Map<String, BigDecimal> map_price = new HashMap<>();
        map_price.put("1", price);
        map_price.put("2", price1);
        map_price.put("3", price2);
        map_price.put("4", price3);
        map_price.put("5", price4);
        map_price.put("6", price5);


        String price_str = JSON.toJSONString(map_price);
        System.out.println(price_str);


        Boolean result = quoteService.add(member, orderNo, map_price, priceDesc);
        return CommonResult.success("报价成功");
    }




    @ApiOperation(value = "报价数量")
    @ResponseBody
    @RequestMapping(value = "/getListCount", method = RequestMethod.GET)
    public CommonResult<Long> getList(String orderNo) throws ServiceException {
        Long count = quoteService.getQuoteCount(orderNo);

        System.out.println("报价数量");

        return CommonResult.success(count);
    }

    @ApiOperation(value = "报价列表")
    @ResponseBody
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public CommonResult<List<TyQuoteResponse>> getList(String orderNo,
                                                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                       @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) throws ServiceException {
        List<TyQuote> list = quoteService.getQuoteList(orderNo, page, pageSize);

        System.out.println("报价列表");




//        List<Person> personList = new ArrayList<>();
//        personList.add(new Person("张三", 1000, 20, "男", "北京"));
//        personList.add(new Person("李四", 2000, 21, "男", "南京"));
//        personList.add(new Person("王五", 3000, 20, "女", "合肥"));
//        personList.add(new Person("赵六", 4000, 22, "男", "四川"));
//        personList.add(new Person("孙七", 5000, 25, "女", "上海"));

        // 将员工工作全部增加10000元
        // 1、方式一：不改变原来员工集合
        List<TyQuoteResponse> listNew = list.stream().map(qQuote -> {
//            TyQuote newQuote = new TyQuote(quote.getName(), 0, 0, null, null);
            TyQuote newQuote = new TyQuote();
            newQuote = qQuote;

            Map<String, BigDecimal> map_price = new HashMap<>();
            map_price.put("1", qQuote.getPrice());
            map_price.put("2", qQuote.getPrice1());
            map_price.put("3", qQuote.getPrice2());
            map_price.put("4", qQuote.getPrice3());
            map_price.put("5", qQuote.getPrice4());
            map_price.put("6", qQuote.getPrice5());

            newQuote.setPrices(map_price);



            TyQuoteResponse tyQuoteResponse  = new TyQuoteResponse();
            tyQuoteResponse.setTyQuote(newQuote);


//            UmsMember member = new UmsMember();
            UmsMember member =  memberService.getById(qQuote.getMemberId());
            member.setPassword(null);


            Number  score = 4.7;
            member.setScore(score);



            tyQuoteResponse.setMember(member);


            return tyQuoteResponse;
        }).collect(Collectors.toList());
//        System.out.println("一次改动前：" +list);
//        System.out.println("一次改动后：" + listNew.get(0).getName() + ">>>" + listNew.get(0).getSalary());




        return CommonResult.success(listNew);
    }
}
